#Descriptive plot
library(plyr); library(dplyr)
library(tidyverse)
library(data.table)
library(modelsummary)
library(ggpubr)
#Read in most recent version of cleaned data using:
#Clean_National_Sample.R
if(Sys.info()[7] == "maclockhart"){
  setwd("/Users/maclockhart/Dropbox/School/RA Work/'22 Seth & Thad/MIT Evolving Election Administration Landscape")
}


for(state in c("Colorado", "Georgia", "LA", "Texas")){
  DF<- fread(paste0("Survey Data/", state, " Recoded.csv"))
  DF<-DF[bot==0]
  #Code outcome variables as change from pre-treatment values
  DF$ownstate <- DF$ownstatepost-DF$ownstatepre
  DF$otherstates <- DF$otherstatepost - DF$otherstatepre
  DF$votefraud <- DF$votefraudpost - DF$votefraudpre
  DF$officialfraud <- DF$officialfraudpost - DF$officialfraudpre
  DF$vote2024 <- DF$vote2024post - DF$vote2024pre
  
  #relevel treatment variable in Colorado to compare treatments to the control
  if(state=="Colorado"){DF$state_treatment <- factor(DF$state_treatment, levels=c("Control", "Colorado - Absentee Voting", "Colorado - Facts"))}
  
  #Pooled treatment effects by video
  m1.1 <- lm(ownstate ~ d_state_treatment, DF, weights=the.wts)
  m1.2 <- lm(otherstates ~ d_state_treatment, DF, weights=the.wts)
  m1.3 <- lm(votefraud ~ d_state_treatment, DF, weights=the.wts)
  m1.4 <- lm(officialfraud ~ d_state_treatment, DF, weights=the.wts)
  m1.5 <- lm(vote2024 ~ d_state_treatment, DF, weights=the.wts)
  
  assign(paste0(state, "_m1.1"), m1.1)
  assign(paste0(state, "_m1.2"), m1.2)
  assign(paste0(state, "_m1.3"), m1.3)
  assign(paste0(state, "_m1.4"), m1.4)
  assign(paste0(state, "_m1.5"), m1.5)
}

#Pooled models
#Load and merge data
DF <- fread("Survey Data/National Recoded.csv")
CO <- fread("Survey Data/Colorado Recoded.csv")
GA <- fread("Survey Data/Georgia Recoded.csv")
LA <- fread("Survey Data/LA Recoded.csv")
TX <- fread("Survey Data/Texas Recoded.csv")
DF$bot <- 0
DF$n<-1:nrow(DF)
DF$sample<-"USA"
CO$sample<-"CO"
GA$sample<-"GA"
LA$sample <- "LA"
TX$sample <-"TX"
DF<-rbindlist(list(DF, CO, GA, LA, TX), fill=T)

#Function to summarize data for plotting
summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                      conf.interval=.95, .drop=TRUE) {
  library(plyr)
  
  # New version of length which can handle NA's: if na.rm==T, don't count them
  length2 <- function (x, na.rm=FALSE) {
    if (na.rm) sum(!is.na(x))
    else       length(x)
  }
  
  # This does the summary. For each group's data frame, return a vector with
  # N, mean, and sd
  datac <- ddply(data, groupvars, .drop=.drop,
                 .fun = function(xx, col) {
                   c(N    = length2(xx[[col]], na.rm=na.rm),
                     mean = mean   (xx[[col]], na.rm=na.rm),
                     sd   = sd     (xx[[col]], na.rm=na.rm)
                   )
                 },
                 measurevar
  )
  
  # Rename the "mean" column    
  datac <- rename(datac, c("mean" = measurevar))
  
  datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean
  
  # Confidence interval multiplier for standard error
  # Calculate t-statistic for confidence interval: 
  # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
  ciMult <- qt(conf.interval/2 + .5, datac$N-1)
  datac$ci <- datac$se * ciMult
  
  return(datac)
}

#Summarize and plot own state trust levels before treatment
df1 <- summarySE(DF, measurevar="ownstatepre", groupvars=c("sample","party"), na.rm=T)
df1$sample<-dplyr::recode(df1$sample, "USA" = "National", "CO" = "Colorado",
                          "GA" = "Georgia", "LA" = "Los Angeles", "TX" = "Texas")
df1$sample <- factor(df1$sample, levels=c("National", "Colorado", "Georgia",
                                          "Los Angeles", "Texas"))

p1<-ggplot(df1, aes(x=party, y=ownstatepre, fill=sample)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=ownstatepre-se, ymax=ownstatepre+se),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),
  )+ scale_fill_grey(start = 0.2, end = .9) +
  #scale_fill_manual(values=c("blue", "green", "red")) +
  xlab("Party") + ylab("Trust in Own State") + labs(fill = "Sample", 
                                                     title="How much do you trust the accuracy and integrity of elections in your own state?") +
  coord_cartesian(ylim=c(1,4))+
  geom_text(
    aes(label = round(ownstatepre, 2)),
    colour = "black", size = 3,
    vjust = -1.4, position = position_dodge(.9)
  ) + theme_bw()

df2 <- summarySE(DF, measurevar="otherstatepre", groupvars=c("sample","party"), na.rm=T)
df2$sample<-dplyr::recode(df2$sample, "USA" = "National", "CO" = "Colorado",
                          "GA" = "Georgia", "LA" = "Los Angeles", "TX" = "Texas")
df2$sample <- factor(df2$sample, levels=c("National", "Colorado", "Georgia",
                                          "Los Angeles", "Texas"))

p2<-ggplot(df2, aes(x=party, y=otherstatepre, fill=sample)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=otherstatepre-se, ymax=otherstatepre+se),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),
  )+scale_fill_grey(start = 0.2, end = .9) +
  # scale_fill_manual(values=c("blue", "green", "red")) +
  xlab("Party") + ylab("Trust in Other States") + labs(fill = "Sample", 
                                                        title="How much do you trust the accuracy and integrity of elections in other states?") +
  coord_cartesian(ylim=c(1,4))+
  geom_text(
    aes(label = round(otherstatepre, 2)),
    colour = "black", size = 3,
    vjust = -1.4, position = position_dodge(.9)
  ) + theme_bw()
ggarrange(p1, p2, ncol=1, nrow=2)
ggsave("Mac and Jen Results/Plots/trust_party_partyfill.png",
       width=7, height=5)

#Graph version for the appendix
df1 <- summarySE(DF, measurevar="ownstatepre", groupvars=c("sample","party"), na.rm=T)
df1$sample<-dplyr::recode(df1$sample, "USA" = "National", "CO" = "Colorado",
                     "GA" = "Georgia", "LA" = "Los Angeles", "TX" = "Texas")
df1$sample <- factor(df1$sample, levels=c("National", "Colorado", "Georgia",
                                              "Los Angeles", "Texas"))

p1<-ggplot(df1, aes(x=sample, y=ownstatepre, fill=party)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=ownstatepre-se, ymax=ownstatepre+se),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),
                )+ scale_fill_grey(start = 0.2, end = .9) +
  #scale_fill_manual(values=c("blue", "green", "red")) +
  xlab("Sample") + ylab("Trust in Own State") + labs(fill = "Party", 
                                                    title="How much do you trust the accuracy and integrity of elections in your own state?") +
  coord_cartesian(ylim=c(1,4))+
  geom_text(
    aes(label = round(ownstatepre, 2)),
    colour = "black", size = 3,
    vjust = -1.4, position = position_dodge(.9)
  ) + theme_bw()

df2 <- summarySE(DF, measurevar="otherstatepre", groupvars=c("sample","party"), na.rm=T)
df2$sample<-dplyr::recode(df2$sample, "USA" = "National", "CO" = "Colorado",
                     "GA" = "Georgia", "LA" = "Los Angeles", "TX" = "Texas")
df2$sample <- factor(df2$sample, levels=c("National", "Colorado", "Georgia",
                                              "Los Angeles", "Texas"))

p2<-ggplot(df2, aes(x=sample, y=otherstatepre, fill=party)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=otherstatepre-se, ymax=otherstatepre+se),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),
  )+scale_fill_grey(start = 0.2, end = .9) +
  # scale_fill_manual(values=c("blue", "green", "red")) +
  xlab("Sample") + ylab("Trust in Other States") + labs(fill = "Party", 
                                                       title="How much do you trust the accuracy and integrity of elections in other states?") +
  coord_cartesian(ylim=c(1,4))+
  geom_text(
    aes(label = round(otherstatepre, 2)),
    colour = "black", size = 3,
    vjust = -1.4, position = position_dodge(.9)
  ) + theme_bw()
ggarrange(p1, p2, ncol=1, nrow=2)
ggsave("Mac and Jen Results/Plots/trust_party_partyfill_appendix.png",
       width=7, height=5)
